<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22
 * Time: 15:49
 */
namespace app\index\controller;
use app\index\model\Goods;
use think\App;
use think\Request;
use think\facade\Session;
class Jxcgoods extends Common{
    public function __construct(App $app = null, Request $request){
        parent::__construct($app, $request);
    }
    //查询商品
    public function getone(){
        $post=$this->param;
        $data['code']='1002';
        $data['msg']='';
        if (!empty($post['number'])) {
            $where=array('number' => $post['number']);
            $re=Goods::getone($where);
            if ($re) {
                $data['code']='1001';
                $data['msg']='';
                $data['data']=$re;
            }else{
                $data['msg']='未查询到该商品';
            }
        }else{
            if (!empty($post['code'])) {
                $where=array('code' => $post['code']);
                $re=Goods::getone($where);
                if ($re) {
                    $data['code']='1001';
                    $data['msg']='';
                    $data['data']=$re;
                }else{
                    $url='http://www.mxnzp.com/api/barcode/goods/details';
                    $post_data['barcode']=$post['code'];
                    $post_data=json_encode($post_data);
                    $result = request_post($url, $post_data);
                    $result=json_decode($result,true);
                    if ($result['code']==1) {
                        $add_data['cid']=8;
                        $add_data['title']=$result['data']['goodsName'];
                        $add_data['price']=$result['data']['price'];
                        $add_data['code']=$result['data']['barcode'];
                        $gid=Goods::add($add_data);
                        if ($gid) {
                            $where=array('id' => $gid);
                            $res=Goods::getone($where);
                            $data['code']='1001';
                            $data['msg']='';
                            $data['data']=$res;
                        }
                    }else{
                        $data['msg']='未查询到该商品';
                    }
                }
            }
        }
        return $this->jiami($data);
    }
    //查询全部商品
    public function getall(){
        $post=$this->param;
        $data['code']='1001';
        $data['msg']='';
        $re=Goods::getall(array('audit' => 1));
        $data['data']=$re;
        return $this->jiami($data);
    }
}

